package cn.ucai.blackfox.fangclass.model.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import cn.ucai.blackfox.fangclass.model.domin.User;

/**
 * Created by Administrator on 2017/3/13 0013.
 */

public class DBManager {
    private static DBOpenHelper dbOpenHelper;

    public DBManager() {

    }

    public static void createDbOpenHelper(Context context) {
        dbOpenHelper = DBOpenHelper.getInstance(context);
    }


    private static DBManager instance;

    public static DBManager getInstance() {
        if (instance == null) {
            synchronized (DBManager.class) {
                if (instance == null) {
                    instance = new DBManager();
                }
            }
        }
        return instance;
    }


    public boolean saveUser(User user) {
        SQLiteDatabase db = null;
        if (dbOpenHelper != null) {
            db = dbOpenHelper.getWritableDatabase();
        }
        if (db.isOpen()) {
            ContentValues values = new ContentValues();
            values.put(UserDao.USER_COLUMN_NAME, user.getMuserName());
            values.put(UserDao.USER_COLUMN_NICK, user.getMuserNick());
            values.put(UserDao.USER_COLUMN_AVATAR, user.getMavatarId());
            values.put(UserDao.USER_COLUMN_AVATAR_PATH, user.getMavatarPath());
            values.put(UserDao.USER_COLUMN_AVATAR_TYPE, user.getMavatarType());
            values.put(UserDao.USER_COLUMN_AVATAR_SUFFIX, user.getMavatarSuffix());
            values.put(UserDao.USER_COLUMN_AVATAR_UPDATE_TIME, user.getMavatarLastUpdateTime());
            return db.replace(UserDao.USER_TABLE_NAME, null, values) != -1;
        }
        return false;
    }
    public User getUser(String username) {
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
        String sql = "SELECT*FROM" + UserDao.USER_TABLE_NAME +
                "WHERE" + UserDao.USER_COLUMN_NAME + "=?";
        if (db.isOpen()) {
            Cursor cursor = db.rawQuery(sql, new String[]{username});
            if (cursor.moveToNext()) {
                User user = new User();
                user.setMuserName(cursor.getString(cursor.getColumnIndex(UserDao.USER_COLUMN_NAME)));
                user.setMuserNick(cursor.getString(cursor.getColumnIndex(UserDao.USER_COLUMN_NICK)));
                user.setMavatarId(cursor.getInt(cursor.getColumnIndex(UserDao.USER_COLUMN_AVATAR)));
                user.setMavatarPath(cursor.getString(cursor.getColumnIndex(UserDao.USER_COLUMN_AVATAR_PATH)));
                user.setMavatarType(cursor.getInt(cursor.getColumnIndex(UserDao.USER_COLUMN_AVATAR_TYPE)));
                user.setMavatarSuffix(cursor.getString(cursor.getColumnIndex(UserDao.USER_COLUMN_AVATAR_SUFFIX)));
                user.setMavatarLastUpdateTime(cursor.getString(cursor.getColumnIndex(UserDao.USER_COLUMN_AVATAR_UPDATE_TIME)));
                return user;
            }
        }
        return null;
    }

}
